package com.zzyl.intercept;

import com.zzyl.constant.Constants;
import com.zzyl.constant.UserCacheConstant;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.StringUtils;
import com.zzyl.utils.UserThreadLocal;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;
import org.springframework.util.AntPathMatcher;

@Component
public class AdminUserInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private AntPathMatcher antPathMatcher = new AntPathMatcher();//模糊匹配工具类

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取token
        String token = request.getHeader(Constants.USER_TOKEN);
        if (StringUtils.isEmpty(token)) {//没有token
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            //throw new BaseException(BasicEnum.LOGIN_LOSE_EFFICACY);
            return false;
        }
        try {
            //解析token
            Claims claims = JwtUtil.parseJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey(), token);
            Long userId = claims.get(Constants.JWT_USERID, Long.class);
            String username = claims.get(Constants.JWT_USERNAME, String.class);
//            String uri = request.getRequestURI();//获取请求路径
//            String method = request.getMethod();//获取请求方式
//            //获取资源列表
//            Set<String> allResourceList = stringRedisTemplate.opsForSet().members(UserCacheConstant.USER_RESOURCE_LIST + userId);
//            boolean flag =false;
//            //匹配
//            if(!CollectionUtils.isEmpty(allResourceList)){
//                for (String url : allResourceList) {
//                    boolean match = antPathMatcher.match(url, method + uri);
//                    if (match) {
//                        flag=true;
//                        UserThreadLocal.setSubject(String.valueOf(userId));
//                        break;
//                    }
//                }
//            }
//            //匹配失败即没有权限
//            if (!flag) {
//                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
//                //throw new BaseException(BasicEnum.SECURITY_ACCESSDENIED_FAIL);
//                return false;
//            }
            UserThreadLocal.setSubject(String.valueOf(userId));
            return true;
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            //throw new BaseException(BasicEnum.LOGIN_LOSE_EFFICACY);
            return false;
        }
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
